package com.dalonedrow.module.hq.ui.views;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.dalonedrow.engine.sprite.base.SimpleColor;
import com.dalonedrow.engine.sprite.base.SimplePoint;
import com.dalonedrow.engine.sprite.base.SimpleVector2;
import com.dalonedrow.engine.sprite.base.SpriteBase;
import com.dalonedrow.engine.sprite.base.SpriteImageObject;
import com.dalonedrow.engine.sprite.base.SpriteImageObjectFactory;
import com.dalonedrow.engine.sprite.impl.BasicSpriteObject;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.base.Action;
import com.dalonedrow.engine.systems.base.InputEvent;
import com.dalonedrow.engine.systems.base.ProjectConstants;
import com.dalonedrow.engine.ui.base.GuiComponent;
import com.dalonedrow.engine.ui.base.View;
import com.dalonedrow.engine.ui.base.menu.MenuGroup;
import com.dalonedrow.engine.ui.base.menu.MenuItem;
import com.dalonedrow.engine.ui.base.menu.VerticalMenu;
import com.dalonedrow.engine.ui.base.panel.CardPanel;
import com.dalonedrow.engine.ui.base.panel.LandscapePagePanel;
import com.dalonedrow.engine.ui.base.panel.PagePanel;
import com.dalonedrow.engine.ui.base.panel.Panel;
import com.dalonedrow.engine.ui.base.panel.PortraitPagePanel;
import com.dalonedrow.engine.ui.impl.Label;
import com.dalonedrow.engine.ui.impl.UIFactory;
import com.dalonedrow.engine.ui.impl.border.EmptyBorder;
import com.dalonedrow.engine.ui.impl.border.H1SelectionBorder;
import com.dalonedrow.engine.ui.impl.border.VXBorder;
import com.dalonedrow.globalvariables.constants.hq.GlobalConstants;
import com.dalonedrow.module.hq.DawnQuestGame;
import com.dalonedrow.module.hq.systems.HQController;
import com.dalonedrow.pooled.PooledStringBuilder;
import com.dalonedrow.pooled.StringBuilderPool;
import com.dalonedrow.utils.UtilityMethods;

/**
 * The title screen view.
 * @author Donald
 */
public final class IntroView implements View {
	/** the title label. */
	private Label		label;
	/** the title screen menu. */
	private MenuGroup	menu;
	/** the panel used to organize menu items. */
	private PagePanel	panel;
	/** the description panel. */
	private CardPanel	pnlDesc;
	/** flag indicating initialization has been completed. */
	private boolean		runInit	= false;
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMouseOver(final float x, final float y) {
		panel.handleMouseOver(x, y);
		/*
		switch (menu.getHighlightedIndex()) {
		case 0:
			pnlDesc.displayChild("MONO");
			pnlDesc.setVisible(true);
			break;
		case 1:
			pnlDesc.displayChild("ISO");
			pnlDesc.setVisible(true);
			break;
		case 2:
			pnlDesc.displayChild("SNES");
			pnlDesc.setVisible(true);
			break;
		default:
			pnlDesc.setVisible(false);
			break;
		}
		*/
	}
	/**
	 * Initializes the {@link View}.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		initTitle();
		initMenu();
		initDescriptionPanel();
		panel = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
		panel.addChild(label);
		Panel middle = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		middle.addChild(menu);
		//middle.addChild(pnlDesc);
		panel.addChild(middle);
		SimplePoint pt = UtilityMethods.getInstance().centerComponentInFrame(
				panel);
		panel.setPosition(pt.getX(), pt.getY(), 0.0f);
		panel.setVisible(true);
		pnlDesc.setVisible(false);
		runInit = true;
	}
	/** Initializes the description panel. */
	private void initDescriptionPanel() {
		pnlDesc = new CardPanel();
		pnlDesc.setBorder(new VXBorder());
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		sb.append("A fantasy adventure set deep within a\n");
		sb.append("hidden stone labyrinth.  This maze of\n");
		sb.append("undergound  rooms  and  corridors  is\n");
		sb.append("controlled   by   the  Evil  Sorceror\n");
		sb.append("Nograz   and  his  forces  of  Chaos.");
		Label lbl = UIFactory.getInstance().getStandardH1Label(sb.toString());
		pnlDesc.addChild("MONO", lbl);

		sb.setLength(0);
		sb.append("ISo");
		lbl = UIFactory.getInstance().getStandardH1Label(sb.toString());
		pnlDesc.addChild("ISO", lbl);

		sb.setLength(0);
		sb.append("With the destruction of Imrryr\n");
		sb.append("and the exile of the Melniboneans,\n");
		sb.append("economic activity moves south.\n");
		sb.append("From the lush coasts of Dharijor\n");
		sb.append("to the barren plains of the\n");
		sb.append("Weeping Waste, seek glory and\n");
		sb.append("adventure in the Young Kingdoms.");
		lbl = UIFactory.getInstance().getStandardH1Label(sb.toString());
		pnlDesc.addChild("SNES", lbl);
		pnlDesc.addChild("blank",
				UIFactory.getInstance().getStandardH1Label(""));
		sb.returnToPool();
		lbl = null;
	}
	/**
	 * Initializes the start menu.
	 * @throws Exception if an error occurs
	 */
	private void initMenu() throws Exception {
		menu = new VerticalMenu();
		menu.setBorder(new EmptyBorder(8, 8, 8, 8));
		MenuItem item1 = new MenuItem(
				"fairlight_14",
				"Start"
				);
		item1.setAlignment(GuiComponent.CENTER, GuiComponent.CENTER);
		item1.setHighlightBorder(new H1SelectionBorder());
		item1.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					// add call to load character chooser screen to input events
					InputEvent.getInstance().addAction(
							ProjectConstants.getInstance(), // object
							HQController.class.getMethod(
									"loadCharChooser", null),
							null
							);
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		menu.addItem(item1);
		MenuItem item4 =  new MenuItem(
				"fairlight_14",
				"Exit"
				);
		item4.setAlignment(GuiComponent.CENTER, GuiComponent.CENTER);
		item4.setHighlightBorder(new H1SelectionBorder());
		item4.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				DawnQuestGame.getInstance().end();
			}
		});
		menu.addItem(item4);
		menu.highlightIndex(0);
	}
	/**
	 * Initializes the title image.
	 * @throws Exception if an error occurs
	 */
	private void initTitle() throws Exception {
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"title");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		label = new Label(sb);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean keyTyped(final char character) {
		switch (character) {
		case Keys.ESCAPE:
			System.out.println("esc");
			DawnQuestGame.getInstance().end();
			break;
		case Keys.UP:
			menu.highlightPrevious();
			break;
		case Keys.DOWN:
			menu.highlightNext();
			break;
		case Keys.ENTER:
			if (menu != null
					&& menu.isVisible()
					&& menu.getHighlighted() != null) {
				menu.getHighlighted().onSelect();
			}
			break;
		case Keys.TAB:
			if (menu != null) {
				if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)
						|| Gdx.input.isKeyPressed(Keys.SHIFT_RIGHT)) {
					menu.highlightPrevious();
				} else {
					menu.highlightNext();
				}
			}
			break;
		case Keys.BACKSPACE:
			try { } catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		case Keys.SPACE:
			try {
				// textfield.addCharacter(e.getKeyChar());
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		default:
			if (Character.isLetterOrDigit(character)) {
				try {
					// textfield.addCharacter(e.getKeyChar());
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
			break;
		}
		return true;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		if (!runInit) {
			try {
				init();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
		panel.prepareForRendering();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean touchUp(final int screenX, final int screenY,
			final int pointer, final int button) {
		return panel.touchUp(screenX, screenY, pointer, button);
	}
}
